package com.yst.sms.impl;

import cn.hutool.http.*;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yst.sms.Sms;
import com.yst.sms.SmsInterface;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class KytSms implements SmsInterface {

    private String appKey;
    private String secretKey;

    @Override
    public void sendSms(String phone,String templateId, String... values) {
        Map<String, Object> params=new HashMap<>();
        params.put("appkey", appKey);
        params.put("appsecret", secretKey);
        params.put("templateid",templateId);
        params.put("phone",phone);
        params.put("templateparams", Arrays.asList(values));
        String result=HttpUtil.post("https://www.qi63.com/SMSServer/smssend", JSONUtil.toJsonStr(params));
        JSONObject jsonObject=JSONUtil.parseObj(result);
        if (jsonObject.containsKey("code")){
            if (!(jsonObject.getInt("code")==0)){
                throw new ExceptionSmsSend(jsonObject.getStr("msg"));
            }
        }
    }

    @Override
    public void sendFullSms(String phone,String templateId, String context) {
        Map<String,String> params=new HashMap<>();
        params.put("appkey", appKey);
        params.put("appsecret", secretKey);
        params.put("templateid",templateId);
        params.put("phone",phone);
        String result=HttpUtil.post("https://www.qi63.com/SMSServer/sendFullTextSms", JSONUtil.toJsonStr(params));
        JSONObject jsonObject=JSONUtil.parseObj(result);
        if (jsonObject.containsKey("code")){
            if (!(jsonObject.getInt("code")==0)){
                throw new ExceptionSmsSend(jsonObject.getStr("msg"));
            }
        }
    }

    @Override
    public void config(String appId, String secretKey) {
        this.appKey=appId;
        this.secretKey=secretKey;
    }

    static {
        Sms.getInstance().register("kyt",new KytSms());
    }
}
